package m.jclecai.com.fragment.refreshableList;


import android.content.Context;
import android.support.annotation.LayoutRes;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.activity.follow.HotPeopleInfoActivity;
import m.jclecai.com.adapter.base.DefaultViewHolderAdapter;
import m.jclecai.com.adapter.holder.DefaultViewHolder;
import m.jclecai.com.bean.Response.ResHotPeople;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.ToastUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;
import m.jclecai.com.view.rounded.RoundedImageView;

/**
 * Created by Dinosa on 2017/11/24.
 */

public class HotPeopleListFragment extends RefreshableListFragment {


    @Override
    public void requestData() {
        if(mPage==1 && mAdapter.getCount()==0){
            showDialog();
        }
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("page",mPage+"");

        HttpManager.getAsyn(Constant_Url.HOT_PEOPLE_LIST, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                ToastUtil.showToast(getActivity(), "网络连接异常!!!");
                mPrListview.onRefreshComplete();
                dissDialog();

            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                mPrListview.onRefreshComplete();

                if (!TextUtils.isEmpty(response)) {
                    ResHotPeople resHotPeople = FastOk.get(response, ResHotPeople.class);
                    if ("0".equals(resHotPeople.getStatus())) {

                        mAdapter.addAll(resHotPeople.getRecords());

                        int totalPages = Integer.parseInt(resHotPeople.getPages());
                        if (mPage < totalPages) {
                            mPage += 1;
                        }
                    } else {
                        DisPlay(resHotPeople.getMessage());
                    }
                } else {
                    DisPlay("网络异常!!!");
                }
            }
        });
    }

    @Override
    protected DefaultViewHolderAdapter createAdapter() {
        return new HotpeopleAdapter(getActivity());
    }


    public class HotpeopleAdapter extends DefaultViewHolderAdapter<ResHotPeople.HotPeopleBean> {



        public HotpeopleAdapter(Context context) {
            super(context);
        }

        @Override
        public DefaultViewHolder<ResHotPeople.HotPeopleBean> getRealViewHolder(Context context) {
            return new HotPeopleViewHolder(context, R.layout.item_list_concern);
        }
    }

    public class HotPeopleViewHolder extends DefaultViewHolder<ResHotPeople.HotPeopleBean> {


        @BindView(R.id.iv_user_icon)
        RoundedImageView mIvUserIcon;

        @BindView(R.id.tv_username)
        TextView mTvUsername;

        @BindView(R.id.tv_bet_history)
        TextView mTvBetHistory;

        @BindView(R.id.tv_fans_count)
        TextView mTvFansCount;

        @BindView(R.id.tv_unit_award)
        TextView mTvUnitAward;

        @BindView(R.id.tv_is_follow)
        TextView mTvIsFollow;

        @BindView(R.id.tv_follow_number)
        TextView mTvFollowNumber;

        @BindView(R.id.iv_user_rank)
        ImageView mIvUserRank;

        public HotPeopleViewHolder(Context context, @LayoutRes int resource) {
            super(context, resource);
        }

        @Override
        public void init(final ResHotPeople.HotPeopleBean bean,int position) {

            //Html.fromHtml()
            String tvUnitAward="<html><body>单位回报:  <font color='red'>"+bean.getPayback_total()+"</font></body></html>";
            mTvUnitAward.setText(Html.fromHtml(tvUnitAward));

            mTvUsername.setText(bean.getUsername());
            String military = bean.getMilitary_total();
            String[] split = military.split("/");
            mTvBetHistory.setText("近7天战绩:" + split[0] + "中" + split[1]);

            Glide.with(getActivity()).load(bean.getUser_face()).into(mIvUserIcon);
            //这里我们判断是否可以跟单；
            if ("0".equals(bean.getUser_project_follow())) {
                mTvFollowNumber.setVisibility(View.INVISIBLE);
                mTvIsFollow.setEnabled(false);
            } else {
                mTvIsFollow.setEnabled(true);
                mTvFollowNumber.setText(bean.getUser_project_follow());
                mTvFollowNumber.setVisibility(View.VISIBLE);
            }

            if (!TextUtils.isEmpty(bean.getFans())) {
                mTvFansCount.setText("粉丝: "+bean.getFans()+"人");
            }else{
                mTvFansCount.setText("粉丝: 0人");
            }


            if (bean.getUser_rank()>0) {
                mIvUserRank.setVisibility(View.VISIBLE);
            }else{
                mIvUserRank.setVisibility(View.INVISIBLE);
            }

            setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //这里是跳转到具体的详情页面中去；
                    getActivity().startActivity(HotPeopleInfoActivity.getIntent(getActivity(),bean.getUser_id()));
                }
            });

        }
    }
}
